<?php

/**
// +----------------------------------------------------------------------+
// | Desktop-blog                                                         |
// +----------------------------------------------------------------------+
// | Copyright (c) 2008~2010 http://desktop-blog.cn All rights reserved.  |
// +----------------------------------------------------------------------+
// | Desktop-blog 													      |
// +----------------------------------------------------------------------+
// | Author: xuwu125 <xuwu125@gmail.com>                                  |
// +----------------------------------------------------------------------+
// $Id$ lib_main.php


 +------------------------------------------------------------------------------
 * Desktop-blog 后台主体程序工具用类
 +------------------------------------------------------------------------------
 */

class dtb {
    var $db_name='dtb';
    var $db_prefix='dtb_';
	/**
	 * 返回表全名
	 *
	 * @param string $tbname	简单表名
	 * @return string			全名
	 */
	function __construct($db_name,$db_prefix){
	    $this->dtb($db_name,$db_prefix);
	}
	function dtb($db_name,$db_prefix){
	    $this->db_name=$db_name;
	    $this->db_prefix=$db_prefix;
	}
	/**
	 * 构造表名
	 *
	 * @param unknown_type $tbname
	 * @return unknown
	 */
	function table($tbname){
		return "`".$this->db_name."`.`".$this->db_prefix."$tbname`";
	}
	/**
	 * 构造一组由数组组成的完整数据库字段
	 *
	 * @param array $arr		名部数组
	 * @return string			返回一个完整的多个数据库字段名
	 */
	function fieldin($arr){
		if(count($arr)>0){
		$temp=array();
		foreach ($arr as $list){
			$temp[]="`$list`";
		}
		return implode(", ",$temp);
		}else{
		return "";
		}
	}
	
	/**
	 * 自动数据组的值转换成一个SQL字符串
	 *
	 * @param array $arr
	 * @return string
	 */
	function array_autodata($arr){
	    if(count($arr)>0){
		$temp=array();
		foreach ($arr as $list){
			$temp[]="'$list'";
		}
		return implode(", ",$temp);
		}else{
		return "";
		}
	}
    
	function autoupdate($table,$arrays,$where=''){
	    if(!empty($table)&&!empty($arrays)){
	    $temp=array();
	    $sql="UPDATE ".$GLOBALS['dtb']->table($table)." SET ";
	       foreach ($arrays as $key=>$value){
	           if($value!=''){
	               $temp[]="`$key`='$value'";
	           }
	       }
	       
	       $sql.=implode(",",$temp);
	       if(!empty($where)){
	       $sql.=" WHERE ".$where;
	       }
	       
	       return $GLOBALS['db']->query($sql);
	    }
	}
	/**
	 * 装指定的数组作为数组的键转换成一个新的二维数据组
	 *
	 * @param array $arr
	 * @param string $type
	 * @return array
	 */
	function array_data($arr,$type='post'){
	    if(count($arr)>0){
	    $type=strtolower($type);
		$temp=array();
		foreach ($arr as $list){
		    switch ($type){
		        case "post":
		        $temp[]=$_POST[$list]; break;
		        case "get":
		        $temp[]=$_GET[$list]; break;
		        case "request":
		        $temp[]=$_REQUEST[$list]; break;
		        case "ENV":
		        $temp[]=$_POST[$list]; break;
		        case "server":
		        $temp[]=$_SERVER[$list]; break;
		        case "session":
		        $temp[]=$_SESSION[$list]; break;
		        case "cookies":
		        $temp[]=$_COOKIE[$list]; break;
		        case "files":
		        $temp[]=$_FILES[$list]; break;
		    }
		}
		return $temp;
		}else{
		return array();
		}
	}
	function auto_array($arr,$type='post'){
	    if(count($arr)>0){
	    $type=strtolower($type);
		$temp=array();
		foreach ($arr as $list){
		    switch ($type){
		        case "post":
		        $temp[$list]=$_POST[$list]; break;
		        case "get":
		        $temp[$list]=$_GET[$list]; break;
		        case "request":
		        $temp[$list]=$_REQUEST[$list]; break;
		        case "ENV":
		        $temp[$list]=$_POST[$list]; break;
		        case "server":
		        $temp[$list]=$_SERVER[$list]; break;
		        case "session":
		        $temp[$list]=$_SESSION[$list]; break;
		        case "cookies":
		        $temp[$list]=$_COOKIE[$list]; break;
		        case "files":
		        $temp[$list]=$_FILES[$list]; break;
		    }
		}
		return $temp;
		}else{
		return array();
		}
	}
	/**
	 * 自动插入数据到指定的表
	 *
	 * @param array $user_fildes
	 * @param string $tableanme
	 * @return bolle
	 */
	function inser_into($user_fildes,$tableanme){
	    $sql="INSERT INTO ".$this->table($tableanme)." (".$this->fieldin($user_fildes).") ".
        " VALUES (".$this->array_autodata($this->array_data($user_fildes,'post')).")";
        return $GLOBALS['db']->query($sql);
	}
	
	/**
	 * 系统提示信息
	 *
	 * @param string $title
	 * @param string $message
	 * @param array $link
	 */
	function sysbox($title,$message,$link=array()){
	    if(!empty($link)){
	    $linkhtml="<a href='".$link['href']."'>".$link['text']."</a></dt>";
	    $refererhtml='<meta http-equiv="Refresh" content="5;URL='.$link['href'].'" /><script type="text/javascript">window.setTimeout("location.href=\''.$link['href'].'\'",5001);</script>';
	    }
		die('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>'.$GLOBALS['_CFG']['site_name']." ".$title.'</title>
<meta name="keywords" content="'.$GLOBALS['_CFG']['keywords'].'" />
<meta name="description" content="'.$GLOBALS['_CFG']['description'].'" />
<link href="'.$GLOBALS['_CFG']['site_url'].'images/css/default.css" rel="stylesheet" type="text/css" />
'.$refererhtml.'
</head>

<body>'.
		"<div id='error_message' style='width:76%; margin:10% auto; '>
		<dt id='error_message_title'>".$GLOBALS['_CFG']['site_name']."   $title</dt><dt id='error_message_content'>$message
		<li><span  sttyle='color:red'>浏览器将会在5秒钟后自动转向，如果浏览器没有自动转向，请点击下面链接。</span></li></dt>
		<dt id='error_message_link'><a href='#' onclick='history.back();'>返回上一页</a>  $linkhtml</dt></div>
		<div style='display:none'><script src='http://s141.cnzz.com/stat.php?id=957488&web_id=957488&show=pic' language='JavaScript' charset='gb2312'></script></div></body>
</html>");
	}
	/**
	 * 整理模板,输出最终内容
	 *
	 * @param maxdata $data
	 * @param array string $needstr
	 * @param array string $replacestr
	 */
	function outhtml($data,$needstr=null,$replacestr=null){
	    if(!empty($data)){
	         $data=str_replace('../images/',$GLOBALS['_CFG']['site_url'].'images/',$data);
    	    if(empty($needstr)||empty($replacestr)){
    	       echo $data;
    	    }else{
    	    echo str_replace($needstr,$replacestr,$data);
    	    }
	    }
	}
	/**
	 * 写成HTML文件
	 *
	 * @param unknown_type $data
	 * @param unknown_type $path
	 */
	function write_html($data,$path){
	    $handle = @fopen($path, 'a');
        @fwrite($handle, $data);
        @fclose($handle);
	}
	/**
	 * 获取用户的信息
	 *
	 * @param string int $user
	 * @param int $type
	 * @return array
	 */
	function get_user($user,$type=0){
	    if($type=='0'){
	        $sql="SELECT * FROM ".$GLOBALS['dtb']->table("users")." WHERE user_id='$user'";
	    }else{
	        $sql="SELECT * FROM ".$GLOBALS['dtb']->table("users")." WHERE user_name='$user'";
	    }
	    $temp=$GLOBALS['db']->getRow($sql);
	    if(!empty($temp)){
	    $temp['user_emailtemplate']=str_replace("{crlf}","\n",$temp['user_emailtemplate']);
	    }
	    return $temp;
	}
	/**
	 * 自动检查所有字段是否为空
	 *
	 * @param array $a1
	 * @param array $a2
	 * @return bolle
	 */
	function check_null($a1,$a2){
	    foreach ($a2 as $f){
	        
	        if(!isset($a1[$f])){
	            return false;
	        }
	    }
	    return true;
	}
	
	/**
	 * 会员登录检查
	 *
	 */
	function user_login($u){
	    if(empty($u)){
	        $dtb->sysbox("错误提示","<li>对不起,参数错误,请返回首页进入本站！</li>");
	    }else{
	        $sql="SELECT * FROM ".$GLOBALS['dtb']->table("users")." WHERE user_name='".$u['user_name']."' and user_pass='".$u['user_pass']."' ";
	        $l=$GLOBALS['db']->getRow($sql);
	        if($l){
	            if($l['user_look']!=0){
	                return false;
	            }
	            global $ip;
	            $sql="UPDATE ".$GLOBALS['dtb']->table("users")." SET user_lastloginip='$ip',user_lastlogintime='".time()."',user_logincount=user_logincount+1,user_integral=user_integral+'1' WHERE user_id='$l[user_id]'";
	            $GLOBALS['db']->query($sql);
	            $_SESSION['user_id']=$l['user_id'];
	            $_SESSION['user_name']=$l['user_name'];
	            $_SESSION['user_rank']=$l['user_rank'];
	            $_SESSION['user_integral']=$l['user_integral'];
	            $_SESSION['user_sex']=$l['user_sex'];
	            $_SESSION['user_kind']=$l['user_kind'];
	            return $l;
	        }else{
	            return false;
	        }
	    }
	}
	
	function check_user_login(){
	    if(empty($_SESSION['user_id'])){
	        return false;
	    }else{
	        return true;
	    }
	}
	
	/**
	 * 会员退出
	 *
	 */
	function login_out(){
	    $_SESSION['user_id']= $_SESSION['user_name']=$_SESSION['user_rank']=$_SESSION['user_rank']=$_SESSION['user_sex']=null;
	}
	
	function get_message($pmid){
	    $sql="SELECT * FROM ".$GLOBALS['dtb']->table("message")." WHERE `id`='$pmid'";
	    return $GLOBALS['db']->getRow($sql);
	}
	function fopen($fileurl){
	    if(empty($fileurl)){
	        return false;
	    }else{
	        $c=@file_get_contents($fileurl);
	        if($c){
	            return $c;
	        }else{
	            return false;
	        }
	    }
	}
	function fwrite($fileurl,$contents){
	    if(empty($fileurl)){
	        return false;
	    }else{
	        $c=file_put_contents($fileurl,$contents);
	        if($c){
	            return true;
	        }else{
	            return false;
	        }
	    }
	}
	
	/**
	 * 检查会员登录情况，对已经登录的会员进行相对应该的赋值。
	 *
	 * @param int $type
	 */
	function set_user_login($type=0){
	    $login_if=$this->check_user_login();
        if(!$login_if&&$type==0){
            $link=array('text'=>'马上登录','href'=>$GLOBALS['_CFG']['site_url'].'login.html');
            $this->sysbox('登录提示','<li>此操作需要先登录，请先登录！</li>
                <li>成为会员体验找E夜情，找自己的性伴侣的快感！(如果你还没有账号<a href="'.$GLOBALS['_CFG']['site_url'].'reg.html">马上注册</a>  )</li>
                <li>SEX，原来如此美好，也不在遥远。</li>
                <li>还在等什么，你就是我们最好的会员，最受欢迎的就是你！</li>
                <li>享受激情，享受快乐，一切自己做主，COME ON！</li>',$link);
        }else{
           global $tpl;
           if(!$login_if){
               $tpl->assign('islogin',false); 
           }else{
           $tpl->assign('islogin',true); 
           $tpl->assign('user_id',$_SESSION['user_id']); 
           $tpl->assign('user_name',$_SESSION['user_name']); 
           $tpl->assign('user_rank',$_SESSION['user_rank']); 
           $tpl->assign('user_sex',$_SESSION['user_sex']); 
           $tpl->assign('user_integral',$_SESSION['user_integral']); 
           $tpl->assign('user_kind',$_SESSION['user_kind']); 
           }
        }
	}
	
	/**
	 * 获取好友列表和加我的好友列表
	 *
	 * @param int $user_id
	 * @param int $type
	 * @return array
	 */
	function get_my_friend($user_id,$type=0){
	    if($type==0){
	       $sql="SELECT a.id,a.user_id,a.friend_id,a.addtime,b.user_name,b.user_logincount,b.user_id as olduid FROM ".$GLOBALS['dtb']->table("friend")." AS a LEFT JOIN ".$GLOBALS['dtb']->table("users")." AS b ON a.friend_id=b.user_id WHERE a.user_id='$user_id'";
	    }else{
	       $sql="SELECT a.id,a.user_id,a.friend_id,a.addtime,b.user_name,b.user_logincount,b.user_id as olduid FROM ".$GLOBALS['dtb']->table("friend")." AS a LEFT JOIN ".$GLOBALS['dtb']->table("users")." AS b ON a.user_id=b.user_id WHERE a.friend_id='$user_id'";    
	    }
	    return $GLOBALS['db']->getAll($sql);
	}
	
	function mydetil($user_id){
	    $arr=array('messagecount'=>0,'addmyfriendcount'=>0);
	    $sql="SELECT count(*) FROM ".$GLOBALS['dtb']->table('message')." WHERE `type`='0' AND read_user='$user_id' AND isdel='0' AND  read_ok=0 ";
	    $temp=$GLOBALS['db']->getOne($sql);
	    if(!empty($temp)){
	        $arr['messagecount']=$temp;
	    }
	    $sql="SELECT count(*) FROM ".$GLOBALS['dtb']->table('friend')." WHERE `addtime`>='".(time()-3600)."' AND friend_id='$user_id' ";
	    $temp=$GLOBALS['db']->getOne($sql);
	    if(!empty($temp)){
	        $arr['addmyfriendcount']=$temp;
	    }
	    
	    return $arr;
	}
	
	
}

?>
